package com.xcity.record;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.xcity.merge.MergeUtil;

public class Relation2 extends BaseRecord {

	public static String SQL_PARA = "type, srcId, destId, intimacy, time, pool";
	
	public static String COUNT_SQL = "select count(id) from relation2 {0}";
	public static String SELECT_LIMIT = "select * from relation2 {2} limit {0},{1}";
	public static String CONDITION = "WHERE relation2.srcId not IN (SELECT player.id FROM player WHERE player.vipLvl = {0} AND player.`level` < {1} AND player.lastLoginTime < \"{2}\") AND relation2.destId not IN (SELECT player.id FROM player WHERE player.vipLvl = {0} AND player.`level` < {1} AND player.lastLoginTime < \"{2}\")";
	
//	protected long id;
	protected byte type;
	protected long srcId;
	protected long destId;
	protected int intimacy;
	protected Date time;
	protected String pool;
	
	@Override
	public void load(ResultSet rs) throws SQLException {
//		this.id = rs.getLong(1);
		this.type = rs.getByte(2);
		this.srcId = rs.getLong(3);
		this.destId = rs.getLong(4);
		this.intimacy = rs.getInt(5);
		this.time = rs.getTimestamp(6);
		this.pool = rs.getString(7);
	}

	@Override
	public String getColumnNames() {
		return SQL_PARA;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
//		sb.append(MergeUtil.toSqlString(id));
//		sb.append(", ");
		sb.append(MergeUtil.toSqlString(type));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(srcId));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(destId));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(intimacy));
		sb.append(", ");
		sb.append(MergeUtil.toDateString(time));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(pool));
		return sb.toString();
	}

	@Override
	public String toSqlString(){
		switch (status) {
		case STATUS_INSERT:
			{
				StringBuffer sb = new StringBuffer();
				sb.append("INSERT INTO `");
				String tableName = getClass().getSimpleName().toLowerCase();
				sb.append(tableName);
				sb.append('`');
				sb.append(" (");
				sb.append(getColumnNames());
				sb.append(")");
				sb.append(" VALUES (");
				sb.append(toString());
				sb.append(");\n");
				return sb.toString();
			}
		}
		return "";
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public long getSrcId() {
		return srcId;
	}

	public void setSrcId(long srcId) {
		this.srcId = srcId;
	}

	public long getDestId() {
		return destId;
	}

	public void setDestId(long destId) {
		this.destId = destId;
	}

	public int getIntimacy() {
		return intimacy;
	}

	public void setIntimacy(int intimacy) {
		this.intimacy = intimacy;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public String getPool() {
		return pool;
	}

	public void setPool(String pool) {
		this.pool = pool;
	}
	
}
